import {
  selectIncidentalList,
  selectIncidentalDetail,
  removeIncidental,
  generatedBills,
  selectFileList,
  incidentalOperate,
  selectAll,
  addBillConfirm,
  incidentConfirm,
  InfoDownload,
} from '@/services/fleetManage/incidental';
import prompt from '@/components/Prompt';

export default {
  namespace: 'incidental',

  state: {
    incidentalList: {}, // 帐单列表
    incidentalDetail: {},
    formValues: {},
  },

  effects: {
    // 1、杂费  列表
    *selectIncidentalList({ payload, callback }, { call, put }) {
      const response = yield call(selectIncidentalList, payload);
      const { code, data } = response;
      if (code !== 0) return;
      const { list, pageSize, total, pageNum } = data;
      yield put({
        type: 'saveAllValus',
        payload: {
          incidentalList: {
            pagination: {
              current: pageNum,
              pageSize,
              total,
            },
            list,
          },
        },
      });
      callback && callback(list);
    },
    // 2、杂费  详情
    *selectIncidentalDetail({ payload, callback }, { call, put }) {
      const response = yield call(selectIncidentalDetail, payload);
      const { code, data } = response;
      if (code !== 0) return;
      let newData = data;
      const filePayLoad = { bizId: payload.id, fileBizType: 'MISCELLANEOUS' };
      const fileResponse = yield call(selectFileList, filePayLoad);
      if (fileResponse.code == 0) {
        newData.fileList = fileResponse.data;
      }
      console.log('杂费详情newData',newData)
      yield put({
        type: 'detail',
        payload: { incidentalDetail: { [payload.id]: newData } },
      });
      callback && callback(data);
    },
    // 3、杂费 删除
    *removeIncidental({ payload, callback }, { call }) {
      const response = yield call(removeIncidental, payload);
      // console.log('response===',response)
      const { code, data, message } = response;
      const content = `删除${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(true);
    },

    // 4、杂费  操作：新增、编辑
    *incidentalOperate({ payload, callback }, { call }) {
      const response = yield call(incidentalOperate, payload);
      const { code, data, message } = response;
      const content = payload.id ? `编辑${message}` : `新增${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(data);
    },
    //5、生成账单
    *generatedBills({ payload, callback }, { call }) {
      const response = yield call(generatedBills, payload);
      const { code, message, data } = response;
      if (code === 0) {
        prompt({ content: `账单生成${message}!` });
        if (callback) callback(data);
      }

    },
    //6. 加入账单 确认
    *addBillConfirm({ payload, callback }, { call }) {
      const response = yield call(addBillConfirm, payload);
      const { code, message, data } = response;
      if (code === 0) {
        prompt({ content: `加入账单${message}!` });
        if (callback) callback(data);
        callback&&callback(data)
      }
     
    },
    //7. 杂费确认 确认按钮
    *incidentConfirm({ payload, callback }, { call }){
      const response = yield call(incidentConfirm, payload);
      const { code, message, data } = response;
      if (code === 0) {
        prompt({ content: `杂费确认${message}!` });
        if (callback) callback(data);
        callback&&callback(data)
      }
    },
    //8.导出 
    *InfoDownload({payload,callback},{call}){
       yield call(InfoDownload,payload)
    //  console.log('response---',response)
    },
    *selectAll({ payload, callback }, { call }) {
      const response = yield call(selectAll, payload);
      if (response.code === 0) {
        prompt({ content: `账单生成${response.message}!` });
        if (callback) callback(response.data);
      } else {
        if (callback) callback();
      }
    },
    /**
     * 设置表单参数
     */
    *allValus({ payload }, { _, put }) {
      yield put({
        type: 'saveAllValus',
        payload,
      });
    },
  },

  reducers: {
    //设置表单参数
    saveAllValus(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    detail(state, { payload }) {
      let param = '';
      for (let k in payload) {
        param = k;
      }
      return {
        ...state,
        [param]: { ...state[param], ...payload[param] },
      };
    },
  },
};
